package cn.edu.swu.lms.controller;

import cn.edu.swu.lms.db.DatabaseService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;
import jdk.javadoc.doclet.Doclet;

import static cn.edu.swu.lms.db.DatabaseService.CONTEXT_KEY;

@WebListener
public class AppInitializer implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce){
        System.out.println("...........context start");

        DatabaseService service=DatabaseService.getInstance();
        service.init();
        ServletContext context=sce.getServletContext();
        context.setAttribute(CONTEXT_KEY,service);
    }

    public void contextDestroyed(ServletContextEvent sce){
        ServletContext context=sce.getServletContext();
        DatabaseService service=(DatabaseService)context.getAttribute(DatabaseService.CONTEXT_KEY);
        service.closeDataSource();
        System.out.println("...........close DatabaseService");
        System.out.println("...........context destroy");

    }
}
